﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using System.IO.Ports;
using System.Threading;
using System.IO;





namespace jiasudu.Model
{
    class JSDmodel : ObservableObject
    {
        public JSDmodel()
        {
            this.xline = 0;
            this.yline = 0;
            this.zline = 0;
            try
            {
                myport.DataReceived += getdata;
                myport.Open();

            }
            catch (UnauthorizedAccessException)
            {
                System.Windows.MessageBox.Show("无法打开串口");
            }
            catch (IOException)
            {
                System.Windows.MessageBox.Show("串口不存在");
            }
            catch (InvalidOperationException)
            {
                System.Windows.MessageBox.Show("串口已经打开，或被占用");
            }
            SJDL.Enqueue(0);
            SJDL.Enqueue(0);
            SJDL.Enqueue(0);
            SJDL.Enqueue(0);
            SJDL.Enqueue(0);
            SJDL.Enqueue(0);
            SJDL.Enqueue(0);
            SJDL.Enqueue(0);


        }
        int FangDaBeiShu = 2;
        double ZuoBiaoPianYi = 300;
        Queue<double> SJDL = new Queue<double>(8);
        int coe0 = 53;
        int coe1 = 127;
        int coe2 = 127;
        int coe3 = 53;

        void lvbo(double x1, double y1, double z1)
        {
            double ain = 45 * (Math.Abs(x1) + Math.Abs(y1) + Math.Abs(z1)) + 77 * Math.Max((Math.Max(Math.Abs(x1), Math.Abs(y1))), Math.Abs(z1));
            SJDL.Dequeue();
            SJDL.Enqueue(ain);
        }
        double FIR(Queue<double> arr)
        {
            double[] a = SJDL.ToArray();
            return ((a[0] - a[7]) * coe3 + (a[1] - a[6]) * coe2 + (a[2] - a[5]) * coe1 + (a[3] - a[4]) * coe0) / 256;
        }


        private void getdata(object sender, SerialDataReceivedEventArgs e)
        {
            try
            {
                string line = myport.ReadLine();

                string[] a = line.Split(',');
                lvbo(Convert.ToDouble(a[0]), Convert.ToDouble(a[1]), Convert.ToDouble(a[2]));
                fir = Convert.ToInt32( FIR(SJDL));

                xline = ((Convert.ToDouble(a[0]) * 300 / 10000) * FangDaBeiShu + ZuoBiaoPianYi);
                yline = ((Convert.ToDouble(a[1]) * 300 / 10000) * FangDaBeiShu + ZuoBiaoPianYi);
                zline = ((Convert.ToDouble(a[2]) * 300 / 10000) * FangDaBeiShu + ZuoBiaoPianYi);
            }
            catch (FormatException)
            {

            }
            catch (Exception)
            {
                throw; //System.Windows.MessageBox.Show("某些错误");
            }
        }

        SerialPort myport = new SerialPort("COM7", 9600);

        private double _xline;

        public double xline
        {
            get { return _xline; }
            set
            {
                _xline = value;
                RaisePropertyChanged(() => xline);
            }
        }
        private double _yline;

        public double yline
        {
            get { return _yline; }
            set
            {
                _yline = value;
                RaisePropertyChanged(() => yline);
            }
        }
        private double _zline;



        public double zline
        {
            get { return _zline; }
            set
            {
                _zline = value;
                RaisePropertyChanged(() => zline);
            }
        }
        private double _fir;

        public double fir
        {
            get { return _fir; }
            set { _fir = value; RaisePropertyChanged(() => fir); }
        }


    }
}
